home *** CD-ROM | disk | FTP | other *** search
- Path: ras.kmit.sk!rastos
- From: rastos@kmit.sk (Rastislav Stanik)
- Newsgroups: comp.lang.c++
- Subject: HELP: BC 3.1 compiler bug
- Date: 19 Apr 1996 17:14:03 GMT
- Organization: Kmit
- Message-ID: <4l8hkr$d0s@alibaba.kmit.sk>
- NNTP-Posting-Host: sk2eu.eunet.sk
- X-Newsreader: TIN [version 1.2 PL2]
-
- A friend of me sent me a short program which causes Borland C++ 3.1
- to do produce strange code. The source is at the and of this message.
- I compiled it myself and got the same strange result.
- I'd like to know if there is some patch to repair this bug or advice
- what to do.
- Any help is highly appreciated!
-
- PROBLEM:
- Let's have a class SIMPLE with constructor and destructor defined.
- They do nothing just print out some message. And let's have a class
- BIG which has a constructor with one parameter of type SIMPLE.
- Creating an global variable of type BIG causes to invoke destructor
- for the passed parameter TWICE.
- Here is a output produced by that program (comments are mine):
-
- SIMPLE at 8FDE:1004 created (SIMPLE & (8F95:0404))
- //invoked constructor of parameter passed to constuctor of BIG
- simple big
- //invoked constructor BIG
- SIMPLE at 8F95:1004 destroyed
- //invoked destructor for parameter passed to constuctor of BIG
- SIMPLE at 8F95:1004 destroyed
- //what the hell is THIS!!!
-
- Here is the source code:
- --------------------------------cut here---------------------------------
- /* this program produces bug under BC 3.1 */
- /* can anyone help me ? */
-
- #include <stdio.h>
-
- class SIMPLE
- {
- public:
- SIMPLE (void)
- {
- printf ("SIMPLE at %p created (void)\n", this);
- };
- SIMPLE (SIMPLE &s)
- {
- printf ("SIMPLE at %p created (SIMPLE & (%p))\n", this, &s);
- };
- ~SIMPLE (void)
- {
- printf ("SIMPLE at %p destroyed \n", this);
- }
- };
-
- class BIG
- {
- public:
- BIG (SIMPLE s)
- {
- printf ("simple big \n");
- }
- };
-
- SIMPLE simple;
- BIG big (simple);
-
- int main (void)
- {
- return 1;
- }
-
- ---------------------------------end cut here------------------------------
- /* under BC 3.1: (original commmet of my friend)
- SIMPLE at 8F95:0404 created (void) // simple created
- SIMPLE at 8FDE:1004 created (SIMPLE & (8F95:0404)) // copy of simple created
- simple big // big created
- SIMPLE at 8F95:1004 destroyed // copy destroyed #1
- SIMPLE at 8F95:1004 destroyed // copy destroyed #2
- SIMPLE at 8F95:0404 destroyed // simple destroyed
- */
-
- If you are intersted here is disassembling of critical part:
- (I did not checked this but I saw the above lines after my compilation)
-
- @_STCON_$qv proc far
-
- ...
-
- mov ax,offset DGROUP:_simple ; call constuctor for simple
- push ax
- call near ptr @@SIMPLE@$bctr$qv
- pop cx ; free this
-
- sub sp,2 ; alloc room for copy -----
- |
- mov ax,offset DGROUP:_simple ; create copy of simple |
- push ax |
- lea ax,word ptr [bp-2] |
- push ax |
- call near ptr @@SIMPLE@$bctr$qr6SIMPLE |
- pop cx ; free this |
- pop cx ; free address of parameter|
- |
- mov ax,offset DGROUP:_big ; call constructor of big |
- push ax ; which destroy copy #1 |
- call near ptr @@BIG@$bctr$q6SIMPLE ; (it is local variable !) |
- pop cx ; free this |
- pop cx ; free copy of simple ____|
-
- mov ax,2 ; destroy copy #2
- push ax
- lea ax,word ptr [bp-2] ; this address is out of stack
- push ax
- call near ptr @@SIMPLE@$bdtr$qv
- pop cx
- pop cx
-
- ...
-
- @_STCON_$qv endp
-
- --
- bye
- rastos
- ___________________________________________________________________
- | It's not that I'm afraid to die.| PGP public key 0x21A5BF31 on |
- | I just don't want to be there - | keyservers. The fingerprint is: |
- | when it happens. | F2E2E1CC690778698A89A7939874DD95|
- `~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
-